﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;


public partial class Share_CompetitionDetails : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
       
            loadForm();
     

    }
    public void loadForm()
    {
        if (Request["CompetitionID"] != null)
        {
            int competitionID = int.Parse(Request["CompetitionID"].ToString());
            CompetitionDAO comDAO = new CompetitionDAO();
            CompetitionInfo cp = comDAO.getCompetitionID(competitionID);
            IList<CompetitionInfo> list = new List<CompetitionInfo>();
             list.Add(comDAO.getCompetitionID(competitionID));
             dtl_Competition.DataSource = list;
             dtl_Competition.DataBind();
             foreach (DataListItem item in dtl_Competition.Items)
             {
                 DataList datalist = (DataList)item.FindControl("dtl_Awards");
                 AwardDAO ar = new AwardDAO();
                 datalist.DataSource = ar.AwardOfCompetition(competitionID);
                 datalist.DataBind();
             }
        }
    }

    protected void dtl_Competition_EditCommand(object source, DataListCommandEventArgs e)
    {
   
        AwardDAO awaDAO = new AwardDAO();
        AwardInfo award = new AwardInfo();
        int competitionID = int.Parse(Request["CompetitionID"].ToString());
        if (awaDAO.GetAwardOfCompetition(competitionID).Count == 0)
        {

            AwardInfo awrd_Ins = new AwardInfo();
            awrd_Ins.AwardName = "1st";
            awrd_Ins.Prize = "0";
            awrd_Ins.CompetitionId = competitionID;
            AwardInfo awrd_Ins2 = new AwardInfo();
            awrd_Ins2.AwardName = "2nd";
            awrd_Ins2.Prize = "0";
            awrd_Ins2.CompetitionId = competitionID;
            AwardInfo awrd_Ins3 = new AwardInfo();
            awrd_Ins3.AwardName = "3rd";
            awrd_Ins3.Prize = "0";
            awrd_Ins3.CompetitionId = competitionID;

            int i1 = awaDAO.InsertAward(awrd_Ins);
            int i2 = awaDAO.InsertAward(awrd_Ins2);
            int i3 = awaDAO.InsertAward(awrd_Ins3);


        }
   
        dtl_Competition.EditItemIndex = e.Item.ItemIndex;
        loadForm();
    }
    /*
    public void showCalendar(object sender, EventArgs e)
    {

            foreach (DataListItem item in dtl_Competition.Items)
            {
                Calendar calendar = (Calendar)item.FindControl("calendar");
                calendar.Visible = true;
            }

    }
     * */
    protected void dtl_Competition_CancelCommand(object source, DataListCommandEventArgs e)
    {
        dtl_Competition.EditItemIndex = -1;
        loadForm();
    }
    protected void dtl_Competition_UpdateCommand(object source, DataListCommandEventArgs e)
    {
        try
        {
            CompetitionDAO competDAO = new CompetitionDAO();
            CompetitionInfo compet = new CompetitionInfo();
            int competitionID = int.Parse(Request["CompetitionID"].ToString());
            CompetitionInfo cp = competDAO.getCompetitionID(competitionID);
            bool update = true;


            compet.CompetitionID = competitionID;
            compet.CompetitionName = ((TextBox)e.Item.FindControl("txtCompetitionName")).Text;
            compet.Conditions = ((TextBox)e.Item.FindControl("txtCondition")).Text;
            compet.Details = ((TextBox)e.Item.FindControl("txtDetails")).Text;
            compet.StartDate = DateTime.Parse(((TextBox)e.Item.FindControl("txtStartDate")).Text);
            compet.EndDate = DateTime.Parse(((TextBox)e.Item.FindControl("txtEndDate")).Text);
             

            FileUpload file = ((FileUpload)e.Item.FindControl("fuPainting"));

            if (compet.EndDate < compet.StartDate)
            {

                update = false;

            }
            else if (file.HasFile)
            {

                int intFileSizeLimit = 2000;
                string postedFile = file.PostedFile.FileName;
                string extension = System.IO.Path.GetExtension(postedFile);
                string fileName = System.IO.Path.GetFileName(postedFile);
                int size = file.PostedFile.ContentLength / 1024;
                extension = extension.ToLower();

                // if (fuPainting.PostedFile.ContentType == "image/jpeg")
                if (extension.Equals(".jpg") || extension.Equals(".gif"))
                {
                    if (size < intFileSizeLimit)
                    {
                        compet.Url = ("Images/") + file.FileName.ToString();
                        file.PostedFile.SaveAs(Server.MapPath("~/Images/") + fileName);
                     
                    }
                    else
                    {

                        lblMessage.Text = "<br /><br /> <hr />  File size exceeded than limit " + intFileSizeLimit + " KB, Please upload smaller file.";
                        ScriptManager.RegisterStartupScript(this, this.GetType(), "redirectMe", "alert(' File size exceeded than limit');", true);
                    }
                }


                else
                {
                    ScriptManager.RegisterStartupScript(this, this.GetType(), "redirectMe", "alert(' Only image/jpeg file are allowed, try again');", true);
                }
            }
            else
            {
                compet.Url = cp.Url;
              
            }
    
            if (update == true)
            {
                if (file.HasFile)
                {
                    if (System.IO.Path.GetExtension(file.PostedFile.FileName).Equals(".jpg") || System.IO.Path.GetExtension(file.PostedFile.FileName).Equals(".gif"))
                        if (competDAO.UpdateCompetition(compet) > 0)
                        {
                            update_award();
                            dtl_Competition.EditItemIndex = -1;
                            ScriptManager.RegisterStartupScript(this, this.GetType(), "redirectMe", "alert('Update Successfull!');", true);
                            loadForm();
                            lblMessage.Text = "";
                        }
                }
                else
                {
                    if (competDAO.UpdateCompetition(compet) > 0)
                    {
                        update_award();
                        dtl_Competition.EditItemIndex = -1;
                        ScriptManager.RegisterStartupScript(this, this.GetType(), "redirectMe", "alert('Update Successfull!');", true);
                        loadForm();
                        lblMessage.Text = "";
                    }

                    else
                    {
                        ScriptManager.RegisterStartupScript(this, this.GetType(), "redirectMe", "alert('Insert Failed! ');", true);
                      
                    }
                }
            
            }
            else
            {
                ScriptManager.RegisterStartupScript(this, this.GetType(), "redirectMe", "alert('Insert Failed! The EndDate must be greater than StartDate');", true);
        
            }
        }
        catch (Exception ex) { lblMessage.Text = "Insert Failed!!! + Because: +" + ex.Message; }
    }

    public void Cancel_Button(object sender, EventArgs e)
    {
        Response.Redirect("MaintainCompetitions.aspx");
    }


    protected void update_award()
    {
        int competitionID = int.Parse(Request["CompetitionID"].ToString());
        AwardDAO awaDAO = new AwardDAO();
        AwardInfo award = new AwardInfo();

        foreach (DataListItem item in dtl_Competition.Items)
        {
            DataList dtl_Child_Award = (DataList)item.FindControl("dtl_Awards");
            foreach (DataListItem childitem in dtl_Child_Award.Items)
            {
                
                 Label awrdID = childitem.FindControl("lblAwardID") as Label;
                 int awardID = Convert.ToInt32(awrdID.Text);

                 award.AwardId = awardID;
                 award.AwardName = ((Label)childitem.FindControl("txtAwardName")).Text;
                 award.Prize = ((TextBox)childitem.FindControl("txtPrice")).Text;
                 award.CompetitionId = competitionID;
                 awaDAO.UpdateAward(award);
            }
        }

    }

    public int DeleteAllPostByCompetID(int competID)
    {

        PostingDAO postDAO = new PostingDAO();
        int result = postDAO.DeletePostingByCompetID(competID);

        return result;
    }



    protected void dtl_Competition_DeleteCommand(object source, DataListCommandEventArgs e)
    {
        int competitionID = int.Parse(Request["CompetitionID"].ToString());
        CompetitionInfo competInfo = new CompetitionInfo();
        AwardInfo awaInfo = new AwardInfo();
        AwardDAO awaDAO = new AwardDAO();
        CompetitionDAO competDAO = new CompetitionDAO();
        PostingDAO postDAO = new PostingDAO();

        IList<PostingInfo> list = postDAO.GetAllPostingByCompetID(competitionID);

        if (list.Count > 0)
            if (DeleteAllPostByCompetID(competitionID) > 0)
                if (competDAO.DeleteCompetition(competitionID) > 0)
                {
                    ScriptManager.RegisterStartupScript(this, this.GetType(), "redirectMe", "alert('Delete Successfull!');", true);
                    Response.Redirect("MaintainCompetitions.aspx");
                }
                else
                {
                    ScriptManager.RegisterStartupScript(this, this.GetType(), "redirectMe", "alert('Delete Failed!');", true);
                    Response.Redirect("MaintainCompetitions.aspx");
                }
            else
            {
                ScriptManager.RegisterStartupScript(this, this.GetType(), "redirectMe", "alert('Delete  All Posting Failed!');", true);
                Response.Redirect("MaintainCompetitions.aspx");
            }
        else if (competDAO.DeleteCompetition(competitionID) > 0)
        {
            ScriptManager.RegisterStartupScript(this, this.GetType(), "redirectMe", "alert('Delete Successfull!');", true);
            Response.Redirect("MaintainCompetitions.aspx");
        }
    }
}